goog.provide('dkpoints.raid.driver');

goog.require('dkpoints.utils');
goog.require('dkpoints.raid');
goog.require('dkpoints.raid.uploadview');
goog.require('dkpoints.raid.parsedview');

/**
 * Build the UI for the raid.
 * @param {!Object} driverOptions Whether or not to continue editing an already parsed raid.
 */
dkpoints.raid.driver.init = function(driverOptions) {
  dkpoints.utils.loadCSS('raid');
  var me = dkpoints.raid.driver;
  
  if(driverOptions.showUpload) {
    // This can only be called by the admin upload view, but even if someone messes
    // with the authStatus object, they can't access any of the server-side admin APIs.

    // Pass execution off to the Upload View, giving it a callback to let us know
    // when the string has been parsed.
    me.activeView = dkpoints.raid.uploadview;
    me.activeView.loadUI(me.raidParsed);
  }
  else {
    var editable = driverOptions.editAuth ? true : false;
    
    // If we're showing an existing raid, go get it.
    if(driverOptions.showExisting) {
      // Go fetch the raid object from the server.
      console.log("This is where I'd be fetching the raid from the server.");
    }
    else if(driverOptions.showNew) {
      // Build up the raid editting UI using whatever's in dkpoints.raid.driver.raid.
      
      // Make sure there is currently a valid raid object in me.raidOb.
      if( (! me.raidOb) || (! me.raidOb["info"]) || (! me.raidOb["info"]["_parseOrigin"]) ) {
        // Invalid or missing raid object.  Go to the upload page.
        location.hash = "/admin/upload/";
      }
      else {
        // me.raidOb is a valid raid object.  Give it to ParsedView along with
        // the call-back for when the raid changes.
        me.activeView = dkpoints.raid.parsedview;
        dkpoints.raid.parsedview.show(me.raidOb.clone(), editable, me.saveRaid);
      }
    } // end (driverOptions.showNew)
  } // end (! driverOptions.showUpload)
};

/**
 * Callback given to the Upload View as the callback handler after a successful raid parse.
 * @param {Object}newRaid The Raid object that was just parsed.
 */
dkpoints.raid.driver.raidParsed = function(newRaid) {
  var me = dkpoints.raid.driver;
  me.raidOb = newRaid;
  me.teardown();
  
  location.hash = "/admin/upload/new";
};

dkpoints.raid.driver.saveRaid = function(updatedRaid) {
  alert("Raid updated.");
};

dkpoints.raid.driver.teardown = function() {
  var me = dkpoints.raid.driver;
  
  if(me.activeView) {
    me.activeView.teardown();
  }
};
